package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
@Autowired
private SetmealDishService setmealDishService;
@Autowired
private SetmealService setmealService;

    /*
    *
    * 新增菜品，同时保存口味数据
    * */
    @Override
    public void savaDishFlavors(DishDto dishDto) {
        //保存菜品基本信息 dish
        this.save(dishDto);
        //保存口味数据  dishFlavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
        });
        dishFlavorService.saveBatch(flavors);//批量操作
    }

    /*
    * 查询
    * */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //  查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }


    /*
    * 更新
    * */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表的基本信息
            this.updateById(dishDto);
          //清理当前菜品的信息，从dish_flavor表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);//删除

        //添加当前提交过来的口味数据，
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> {
            flavor.setDishId(dishDto.getId());
        });
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void updateStatus(Integer status,List<Long> ids) {
        // 1. 停售操作时，判断当前菜品是否关联的有起售套餐
        if (status.equals(0)) {

            // 根据菜品id查询  套餐菜品表的数据，获取套餐id
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);

            // 判断菜品是否关联的有套餐
            if (CollectionUtils.isNotEmpty(setmealDishList)) {

                // 获取所有的套餐Id  setmealId
                List<Long> setmeals = setmealDishList.stream().map(setmealDish -> setmealDish.getSetmealId()).collect(Collectors.toList());

                // 根据套餐id 查询套餐信息
                LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealLambdaQueryWrapper.in(Setmeal::getId, setmeals);
                setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);

                int count = setmealService.count(setmealLambdaQueryWrapper);

                if (count > 0) {
                    throw new CustomException("菜品有关联的在售套餐，不能直接删除!");
                }
            }

        }

// 2. 更新菜品的状态信息
// 下面这种方法，只能修改状态，并不会走元数据，不会自动修改修改时间和修改人
        LambdaUpdateWrapper<Dish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Dish::getId, ids);
        lambdaUpdateWrapper.set(Dish::getStatus, status);

        this.update(new Dish(),lambdaUpdateWrapper);
    }


/*
* 删除菜品
* */
    @Override
    public void removeWithFlavors(List<Long> ids) {
        //查询菜品状态，确定是否可用删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if(count > 0){
            throw new CustomException("菜品正在售卖中，不能删除");
        }

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId, ids);
        int count1 = setmealDishService.count(setmealDishLambdaQueryWrapper);
        if(count1 > 0){
            throw new CustomException("当前删除的菜品中, 关联着套餐, 不能直接删除");
        }


        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //删除菜品口味数据 ---- dish_flavor
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lambdaQueryWrapper);
    }
}





